package com.nectech.entity;

import com.nectech.client.MapleStoryClient;
import com.nectech.constant.Constant;
import com.nectech.util.ImageUtil;
import sun.nio.cs.ext.MS874;

import java.awt.*;
import java.util.List;
import java.util.Random;

/**
 * @Author: 浅小北
 * @ClassName: Arrow
 * @Description: 弓箭类
 * @Date: 2021/1/28 17:44
 * @Since: version-1.0
 */
public class Arrow extends AbstractMapleStoryObject{
    public static Image[] images = {
            ImageUtil.getImage("hero_left_arrow"),
            ImageUtil.getImage("hero_right_arrow")
    };
    public MapleStoryClient msc;
    public Arrow() {
        this.speed = 10;
        this.live = true;
        this.height = images[0].getHeight(null);
        this.width = images[0].getWidth(null);
    }
    public Arrow(MapleStoryClient msc,int x,int y,Direction dir) {
        this();
        this.msc = msc;
        this.x = x;
        this.y = y;
        this.dir = dir;
    }

     /**
      * @Author: 浅小北
      * @Description: 弓箭类draw方法的重写
      * @Date: 18:02 2021/1/28
      * @Param:
      * @return:
      */
    @Override
    public void draw(Graphics g) {
        if (!live) {
            msc.arrowList.remove(this);
            return;
        }
        switch (dir) {
            case LEFT:
                g.drawImage(images[0],x,y,null);
                break;
            case RIGHT:
                g.drawImage(images[1],x,y,null);
                break;
            default:
                break;
        }
        move();
    }

     /**
      * @Author: 浅小北
      * @Description: 弓箭类move方法的重写
      * @Date: 18:03 2021/1/28
      * @Param:
      * @return:
      */
    @Override
    public void move() {
        if (this.dir == Direction.LEFT) {
            x -= speed;

        }
        if (this.dir == Direction.RIGHT) {
            x += speed;

        }
        outOfBound();
    }

     /**
      * @Author: 浅小北
      * @Description: 弓箭出界问题
      * @Date: 18:47 2021/1/28
      * @Param:
      * @return:
      */
     public void outOfBound() {
         if (this.x < 0 || this.x > Constant.WINDOW_WIDTH - this.width) {
             this.live = false;
         }
     }
     
      /**
       * @Author: 浅小北
       * @Description:  弓箭击打怪物的方法，写的是碰撞逻辑
       * @Date: 20:09 2021/1/28
       * @Param: 
       * @return: 
       */
      public Random random = new Random();
      private boolean hit(Mob mob) {
          /**
           * 注意if里面的条件，第一个是弓箭死亡状态，放置一下掉两次血
           */
          if (this.live && this.getRectangle().intersects(mob.getRectangle()) && mob.action != Action.DIE) {
              this.live = false;
              this.msc.hero.hitValue = (int) (msc.hero.ATT * (1 + random.nextDouble()));//基于攻击力创建攻击数值
              /**
               * 创建出伤害数值来，存到容器中
               */
              Power power = new Power(msc,mob.x - 30,mob.y);
              mob.action = Action.HIT;
              mob.HP -= power.value;
              msc.powerList.add(power);
              if (mob.HP <= 0) {
                  mob.action = Action.DIE;
                  msc.mobList.remove(mob);
                  if (msc.mobList.size() <= 5) {
                      Mob newMob = new Mob(msc,random.nextInt(1000) + 642,718,Direction.LEFT);
                      msc.mobList.add(newMob);
                  }
                  msc.hero.EXP += 100;
                  /**
                   * 怪物死亡爆出道具
                   */
                  if (random.nextInt(100) < 100) {
                      Prop prop = new Prop(msc,mob.x + mob.width / 2 - 15,mob.y + mob.height - 30,random.nextInt(4));
                      msc.propList.add(prop);
                  }
              }
              return true;
          }
          return false;
      }

       /**
        * @Author: 浅小北
        * @Description: hit方法的重载，只做遍历每只怪的事情
        * @Date: 20:13 2021/1/28
        * @Param:
        * @return:
        */
       public boolean hit(List<Mob> mobList) {
           for (int i = 0; i < mobList.size(); i++) {
               Mob mob = mobList.get(i);
               if (hit(mob)) {
                   return true;
               }
           }
           return false;
       }
}
